<template>


  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">

        <el-form-item label="组件名称">
          <el-input placeholder="请输入组件名称" v-model.trim="searchOpsComponentParam.name" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="success" size="mini" @click="getPageListBefore">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveBefore">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>


    <!--列表-->
    <el-card style="margin-top: 10px; min-height: 700px">

      <div class="container">

        <!--一个小框框-->
        <div class="box" v-for="(item, index) in tableData" :key="index">
          <!--角标-->
          <div class="version-tag component-title" v-if="item.mainVersion">v. {{item.mainVersion}}</div>

          <div class="box-content" >

            <el-row :gutter="10">
              <el-col :span="9">
                <div class="block">
                  <el-image class="component-ico" fit="contain" :src="item.icoUrl || require('@/assets/images/ico/app.png')" />
                </div>
              </el-col>
              <el-col :span="15">

                <el-row :gutter="10">
                  <el-col :span="24">
                    <h3 class="component-title"  @click="openComponentLayout(item)">{{item.name}}</h3>
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="8">支持环境：</el-col>
                  <el-col :span="16">
                    <span>{{item.osStr}}</span>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px">
                  <el-col :span="8">安装数量：</el-col>
                  <el-col :span="16">
                    <el-tag size="mini" type="warning">{{item.installNum}}</el-tag>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px; height: 20px; " >
                  <el-col :span="8">官网：</el-col>
                  <el-col :span="16">
                    <el-link class="component-title" type="primary" target="_blank" :href="item.officialWebUrl">
                      {{ item.officialWebUrl }}
                    </el-link>
                  </el-col>
                </el-row>

              </el-col>
            </el-row>

            <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
            <p class="description" @click="openComponentLayout(item)" >{{item.briefIntroduction}}</p>


            <!-- 固定在右下角的按钮组 -->
            <div class="button-group">
              <el-button type="primary" size="mini" @click="openUpdBefore(item)"  >编辑</el-button>
              <el-button type="danger" size="mini" @click="delTips(item)"  >删除</el-button>
            </div>

          </div>
        </div>
      </div>

      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px;"
        background
        layout="total, prev, pager, next, jumper"
        @current-change="pageCurrentChange"
        :page-size="pageConfig.size"
        :current-page="pageConfig.current"
        :pager-count="5"
        :total="pageConfig.total">
      </el-pagination>

    </el-card>



    <ComponentSaveUpd ref="OpsComponentSaveUpdRef" @getOpsComponentPageList="getOpsComponentPageList"/>

  </div>

</template>


<script>

import http from '@/utils/http'
import ComponentSaveUpd from './ComponentSaveUpd.vue'
import ComponentVersionList from './ComponentVersionList.vue'


export default {
  name: "OpsComponentList",
  components: {
    ComponentSaveUpd,
    ComponentVersionList,
  },
  data() {
    return {
      // 查询参数
      searchOpsComponentParam: {
        name: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 20,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据-组件信息
      tableData: [],
      // 当前选中的组件
      selectedComponent: {},
      // 描述列表的css
      CS: {
        // 'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      // 描述列表的css
      LS: {
        // 'color': '#000',
        // 'text-align': 'center',
        'font-weight': '600',
        // 'height': '40px',
        // 'background-color': 'rgba(255, 97, 2, 0.1)',
        'min-width': '110px',
        'word-break': 'keep-all'
      },
      // 人员信息配置
      descriptions: {
        // 列表的尺寸
        size: 'small',
        // 一行 Descriptions Item 的数量
        column: 2,
      },
    }
  },
  methods: {
    /**
     * 渲染图片
     * 只能这样做，图片这里不支持非固定地址返回
     * @param item
     */
    getImageUrlInfo(item) {
      if (!item || !item.ico || item.ico === 'null') {
        // 判空或无效，直接显示默认图
        this.$set(item, 'icoUrl', null);
        return;
      }

      http.getImageUrl(item.ico)
        .then(url => {
          // 异步赋值，触发响应式更新
          this.$set(item, 'icoUrl', url);
        })
        .catch(() => {
          // 下载失败 fallback 默认图
          this.$set(item, 'icoUrl', null);
        });
    },
    /**
     * 打开组件详情
     * @param obj 组件信息
     */
    openComponentLayout(obj){
      this.$router.push({path: '/toComponentLayout', query: {componentId: obj.id}})
    },
    /**
     * 查看
     * @param obj 这一行数据
     */
    openViewBefore(obj){
      this.selectedComponent = obj
      // 解决使用props传值子组件没及时更新的问题，详见：https://blog.csdn.net/qq_14993591/article/details/128733127
      this.$nextTick(() =>{
        this.$refs.ComponentVersionListRef.getOpsComponentVersionPageList()

      })
    },
    /**
     * 打开保存的弹框
     */
    openSaveBefore() {
      this.$refs.OpsComponentSaveUpdRef.init('save', null)
    },
    /**
     * 打开修改之前的的弹出框
     */
    openUpdBefore(obj) {
      this.$refs.OpsComponentSaveUpdRef.init('upd', obj)
    },
    /**
     * 删除之前的提示框
     * @param obj 要删除的数据
     */
    delTips(obj) {
      this.$confirm('是否执行删除, 删除后无法恢复，是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      }).then(() => {
        // 确定删除
        this.delOpsComponent(obj)

      }).catch(() => {
        // 取消删除
      });
    },
    /**
     * 删除
     * @param obj 要删除的数据
     */
    delOpsComponent(obj) {
      http.post("/opsComponent/delOpsComponent", {
        opsComponentId: obj.id,
      }).then(response => {
        let res = response.data;
        if (!res.success) {
          // 401是token过期跳转的
          if (res.code !== 401) {
            this.$message.error(res.msg);
          }
          return
        }

        this.getOpsComponentPageList()
        this.$message.success("删除成功");

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getOpsComponentPageList()
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.searchOpsComponentParam = {
        name: null,
        code: null,
      }
    },
    /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getOpsComponentPageList()
    },
    /**
     * 分页查询列表
     */
    getOpsComponentPageList() {
      this.searchOpsComponentParam.current = this.pageConfig.current
      this.searchOpsComponentParam.size = this.pageConfig.size
      http.post("/opsComponent/getOpsComponentPageList", this.searchOpsComponentParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records
        this.selectedComponent = {}
        // 异步加载每个图片
        this.tableData.forEach(item => this.getImageUrlInfo(item));

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
  },
  // 进入马上执行
  mounted() {
    this.getOpsComponentPageList()
    this.$store.commit('navigationListChange', [
      {name: '组件列表', url: '/toComponentList', query: {}},
    ])
  },
}
</script>

<style scoped>


/**
====== 一个方框 start ===
 */

.container {
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  gap: 20px; /* 小框框之间的间距 */
  padding: 20px;
  min-height: 600px;
}

.component-ico {
  width: 126px;
  height: 110px;
}

.box {
  width: 380px; /* 固定宽度 */
  height: 270px; /* 固定高度 */
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /*overflow: hidden;*/
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  position: relative; /* 添加这个以便version-tag可以绝对定位 */
  overflow: hidden; /* 保持hidden防止内容溢出 */
}

.box-content {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.box-content .description {
  margin-top: 0px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 显示3行 */
  -webkit-box-orient: vertical;
  cursor: pointer;
  margin-bottom: 6px;
}

.component-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  cursor: pointer;
}

/* 角标 start */

.version-tag {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  background: #D4E6F1;
  /*box-shadow: 0 0 0 999px #8FBCDA;*/
  box-shadow: 0 0 0 999px #D4E6F1;
  clip-path: inset(0 -100%);
}


/* 角标 end */

/* 按钮组固定在右下角 */
.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: auto; /* 关键属性，使按钮组固定在底部 */
  /*padding-top: 10px;*/
}

/**
====== 一个方框 end ===
 */




</style>
